package cn.iocoder.yudao.module.handguard.service.publicitynotice;

import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.handguard.controller.admin.publicitynotice.vo.*;
import cn.iocoder.yudao.module.handguard.dal.dataobject.publicitynotice.PublicityNoticeDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.handguard.convert.publicitynotice.PublicityNoticeConvert;
import cn.iocoder.yudao.module.handguard.dal.mysql.publicitynotice.PublicityNoticeMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.handguard.enums.ErrorCodeConstants.*;

/**
 * 通用公示 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class PublicityNoticeServiceImpl implements PublicityNoticeService {

    @Resource
    private PublicityNoticeMapper publicityNoticeMapper;

    @Override
    public Long createPublicityNotice(PublicityNoticeCreateReqVO createReqVO) {
        // 插入
        PublicityNoticeDO publicityNotice = PublicityNoticeConvert.INSTANCE.convert(createReqVO);
        publicityNoticeMapper.insert(publicityNotice);
        // 返回
        return publicityNotice.getId();
    }

    @Override
    public void updatePublicityNotice(PublicityNoticeUpdateReqVO updateReqVO) {
        // 校验存在
        validatePublicityNoticeExists(updateReqVO.getId());
        // 更新
        PublicityNoticeDO updateObj = PublicityNoticeConvert.INSTANCE.convert(updateReqVO);
        publicityNoticeMapper.updateById(updateObj);
    }

    @Override
    public void deletePublicityNotice(Long id) {
        // 校验存在
        validatePublicityNoticeExists(id);
        // 删除
        publicityNoticeMapper.deleteById(id);
    }

    private void validatePublicityNoticeExists(Long id) {
        if (publicityNoticeMapper.selectById(id) == null) {
            throw exception(PUBLICITY_NOTICE_NOT_EXISTS);
        }
    }

    @Override
    public PublicityNoticeDO getPublicityNotice(Long id) {
        return publicityNoticeMapper.selectById(id);
    }

    @Override
    public List<PublicityNoticeDO> getPublicityNoticeList(Collection<Long> ids) {
        return publicityNoticeMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<PublicityNoticeDO> getPublicityNoticePage(PublicityNoticePageReqVO pageReqVO) {
        return publicityNoticeMapper.selectPage(pageReqVO);
    }

    @Override
    public List<PublicityNoticeDO> getPublicityNoticeList(PublicityNoticeExportReqVO exportReqVO) {
        return publicityNoticeMapper.selectList(exportReqVO);
    }

    @Override
    public List<PublicityNoticeDO> getCommonList(String classify) {
        LambdaQueryWrapperX<PublicityNoticeDO> wrapperX = new LambdaQueryWrapperX<>();
        wrapperX.eq(PublicityNoticeDO::getClassify,classify);
        return publicityNoticeMapper.selectList(wrapperX);
    }

    @Override
    public PublicityNoticeDO getCommonListByIndex(int index) {
        PublicityNoticeDO  noticeDO = new PublicityNoticeDO();
        LambdaQueryWrapperX<PublicityNoticeDO> wrapperX = new LambdaQueryWrapperX<>();
        wrapperX.eq(PublicityNoticeDO::getState,0);
        if (index==1){
            wrapperX.eq(PublicityNoticeDO::getId,1705135638082273282l);
            noticeDO = publicityNoticeMapper.selectOne(wrapperX);
        }else if (index==2){
            wrapperX.eq(PublicityNoticeDO::getId,1705135638082273283l);
            noticeDO = publicityNoticeMapper.selectOne(wrapperX);
        }
        return noticeDO;
    }

}
